package com.lh.item.controller;

import com.lh.common.pojo.PageResult;
import com.lh.item.api.GoodsApi;
import com.lh.item.pojo.SkuDo;
import com.lh.item.bo.SpuBo;
import com.lh.item.pojo.SpuDetailDo;
import com.lh.item.pojo.SpuDo;
import com.lh.item.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    final IGoodsService iGoodsService;

    @Autowired
    public GoodsController(IGoodsService iGoodsService) {
        this.iGoodsService = iGoodsService;
    }

    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuList(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                                          @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                                          @RequestParam(value = "key", required = false) String key,
                                                          @RequestParam(value = "saleable", required = false) Boolean saleable) {
        PageResult<SpuBo> spuList = this.iGoodsService.getSpuList(page, rows, key, saleable);
        return ResponseEntity.ok(spuList);
    }

    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetailDo> querySpuDetailBySpuId(@PathVariable(value = "spuId") Long spuId) {
        SpuDetailDo spuDetailDo = this.iGoodsService.getSpuDetailBySpuId(spuId);
        if (null == spuDetailDo) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuDetailDo);
    }

    @GetMapping("/sku/list/{spuId}")
    public ResponseEntity<List<SkuDo>> querySkuListBySpuId(@PathVariable("spuId") Long spuId) {
        List<SkuDo> skus = this.iGoodsService.getSkuListBySpuId(spuId);
        if (CollectionUtils.isEmpty(skus)) {
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }

    @GetMapping("/spu/{spuId}")
    public ResponseEntity<SpuDo> querySpuById(@PathVariable(value = "spuId") Long spuId) {
        SpuDo spudo = this.iGoodsService.querySpuById(spuId);
        if (null == spudo) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spudo);
    }


    /**
     * 新增商品信息
     */
    @PostMapping
    public ResponseEntity<Boolean> addGoods(@RequestBody SpuBo spuBo) {
        boolean success = this.iGoodsService.addGoods(spuBo);
        if (!success) {
            ResponseEntity.badRequest().build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品信息
     */
    @PutMapping
    public ResponseEntity<Boolean> updateGoods(@RequestBody SpuBo spuBo) {
        boolean success = this.iGoodsService.updateGoods(spuBo);
        if (!success) {
            ResponseEntity.badRequest().build();
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
